package com.huiyujiaoyu.powertime;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.KeyEvent;
import android.view.MenuItem;

import com.huiyujiaoyu.base.BaseActivity;
import com.huiyujiaoyu.powertime.ui.column.ColumnFragment;
import com.huiyujiaoyu.powertime.ui.find.FindFragment;
import com.huiyujiaoyu.powertime.ui.home.HomeFragment;
import com.huiyujiaoyu.powertime.ui.my.MyFragment;
import com.huiyujiaoyu.util.AppManager;
import com.huiyujiaoyu.util.ToastUtil;
import com.huiyujiaoyu.widget.ForbidenScrollViewPager;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;

import butterknife.BindView;

import static com.huiyujiaoyu.util.Constant.TAB_SELECT_POS;

public class MainActivity extends BaseActivity {

    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationViewEx mBottomNavigationBar;
    @BindView(R.id.fl_container)
    ForbidenScrollViewPager mFlContainer;
    private int currentPosition;
    @Override
    protected void initView() {
        initBottom();
    }

    @Override
    protected void initListener() {
        super.initListener();
    }


    @Override
    protected void afterSetContentView(Bundle savedInstanceState) {
        super.afterSetContentView(savedInstanceState);
        if (savedInstanceState == null) {
            currentPosition = 0;
            initBottom();
        } else {
            currentPosition = savedInstanceState.getInt(TAB_SELECT_POS);
            mFlContainer.setCurrentItem(currentPosition);
        }
    }

    private void initBottom(){
        mBottomNavigationBar.enableAnimation(true);
        mBottomNavigationBar.enableShiftingMode(false);
        mBottomNavigationBar.enableItemShiftingMode(false);
        mFlContainer.setExpenseOnTouch(true);
        mFlContainer.setForbidenScroll(true);
        mFlContainer.setOffscreenPageLimit(4);
        MainPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager());
        mFlContainer.setAdapter(adapter);
        mBottomNavigationBar.setupWithViewPager(mFlContainer);
        mFlContainer.setCurrentItem(currentPosition);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }
    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ToastUtil.makeShortText(mContext,R.string.press_once_again_exit_app);
                exitTime = System.currentTimeMillis();
            } else {
                AppManager.getAppManager().AppExit();
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
    class MainPagerAdapter extends FragmentPagerAdapter {
        public MainPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = null;
            switch (i) {
                case 0:
                    fragment = HomeFragment.newInstance();
                    break;
                case 1:
                    fragment = ColumnFragment.newInstance();
                    break;
                case 2:
                    fragment = FindFragment.newInstance();
                    break;
                case 3:
                    fragment = MyFragment.newInstance();
                    break;

            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 4;
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(TAB_SELECT_POS, mFlContainer.getCurrentItem());
    }

}
